package org.new9uy.tetris.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.new9uy.tetris.GameConstants;
import org.new9uy.tetris.controller.GUIController;
import org.new9uy.tetris.game.player.Player;
import org.new9uy.tetris.util.SwingUtils;

@SuppressWarnings("serial")
public class PostGamePanel 
    extends JPanel
{
    private JLabel title;
	
    public PostGamePanel(Player player, boolean won)
    {
	super(new BorderLayout());
	setBackground(Color.black);
	setSize(GameConstants.WIN_PREFERRED_DIM);

	createObjects(player, won);
    }

    private void createObjects(Player player, boolean won)
    {
	createTitle(won);
	createGrid(player);
	createButtons();
    }

    private void createTitle(boolean won)
    {
	if(won)
	{
	    createTitle("You win!", Color.green);
	}
	else
	{
	    createTitle("You lose", Color.red);
	}
	
	add(title, "North");
    }

    private void createTitle(String text, Color color)
    {
	title = new JLabel(text);
	Font f = new Font("Dialog", Font.BOLD, 28);
	title.setFont(f);
	title.setAlignmentX(CENTER_ALIGNMENT);
	title.setForeground(color);
    }

    private void createGrid(Player player)
    {
	JPanel parent = new JPanel(new BorderLayout());
	JPanel grid = createFFAGrid(player);

	grid.setBackground(new Color(0, 0, 0, 0));
	parent.setPreferredSize(GameConstants.WIN_POSTGAME_GRID_DIM);
	parent.add(grid);
	add(parent, "Center");
    }

    private JPanel createFFAGrid(Player plyrs)
    {
	JPanel grid = new JPanel(new GridLayout(5, 4, 5, 5));
	addHeaderLine(grid);
	addLine(grid, plyrs);
	return grid;
    }

    private void addHeaderLine(JPanel grid)
    {
	addLine(grid,
		"Rankings:",
		"Score:",
		"Lines:",
		"Level:");
    }

    private void addUnusedLine(JPanel grid)
    {
	addLine(grid, ": ----", "----", "----", "----");
    }

    private void addLine(JPanel grid, Player p)
    {
	if (p == null)
	{
	    addUnusedLine(grid);
	}
	else
	{
	    addLine(grid, p.getName(),
		    p.getScore().getScore(),
		    p.getScore().totalLinesDone(),
		    p.getGravity().getLvl());
	}
    }

    private void addLine(JPanel grid, String name, int scr, int lin, int lvl)
    {
	addLine(grid, name, scr + "", lin + "", lvl + "");
    }

    private void addLine(JPanel grid, String name, String scr, String lin, String lvl)
    {
	grid.add(SwingUtils.newDefaultLabel(name));
	grid.add(SwingUtils.newDefaultLabel(scr));
	grid.add(SwingUtils.newDefaultLabel(lin));
	grid.add(SwingUtils.newDefaultLabel(lvl));
    }

    private void createButtons()
    {
	JPanel flow = new JPanel(new FlowLayout());
	flow.setOpaque(false);
	JButton back = SwingUtils.newDefaultButton("Back");
	JButton exit = SwingUtils.newDefaultButton("Exit");

	back.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(ActionEvent e)
	    {
		//GUIController.current.returnToWelcome();
	    }
	});

	exit.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(ActionEvent e)
	    {
		System.exit(0);
	    }
	});

	flow.add(back);
	flow.add(exit);
	add(flow, "South");
    }
}
